﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.ComControls.Extension;

namespace MINERP.CustomerManage.FtpUpload
{
    public partial class FrmCheckAutoOrder : Form
    {
        //属性
        private static FrmCheckAutoOrder _instance;
        public static FrmCheckAutoOrder GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmCheckAutoOrder();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        //参数
        private BLL.CustomerManage.AutoStartOrderBLL bll = new MINERP.BLL.CustomerManage.AutoStartOrderBLL();
        //int
        public int editTimes = 1;

        public FrmCheckAutoOrder()
        {
            InitializeComponent();
            gridViewOrder.ShowRowIndicator(50);
        }

        private void FrmCheckAutoOrder_Load(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //刷新
        private void tsRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        //审核通过
        private void tsRight_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show("确定审核通过选中的订单吗？", "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    if (gridViewOrder.SelectedRowsCount == 0)
                        return;
                    int[] indexes = gridViewOrder.GetSelectedRows();
                    List<int> orderIds = new List<int>();
                    foreach (int index in indexes)
                    {
                        orderIds.Add(Convert.ToInt32(gridViewOrder.GetRowCellValue(index, "OrderBagID")));
                    }
                    CheckAcross(orderIds);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #region 编辑


        private void tsEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridViewOrder.GetSelectedRows().Length == 1)
                {
                    Cursor = Cursors.WaitCursor;
                    if (editTimes == 1)
                    {
                        int orderId = Convert.ToInt32(gridViewOrder.GetRowCellValue(gridViewOrder.GetSelectedRows()[0], "OrderBagID"));
                        StartOrder.FrmEditOrder frm = new MINERP.CustomerManage.StartOrder.FrmEditOrder(orderId, 0);
                        frm.CheckAutoOrder = this;
                        frm.UserType = 2;                   //表示是自动开单审核
                        editTimes = 2;
                        frm.ShowDialog();
                        Cursor = Cursors.Default;
                    }
                }
            }
            catch (Exception ex)
            {
                editTimes = 1;
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void gridViewOrder_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0)
                {
                    if (e.Clicks == 2)                  //双击
                    {
                        Cursor = Cursors.WaitCursor;
                        int orderId = Convert.ToInt32(gridViewOrder.GetRowCellValue(e.RowHandle, "OrderBagID"));
                        StartOrder.FrmEditOrder frm = new MINERP.CustomerManage.StartOrder.FrmEditOrder(orderId, 0);
                        frm.CheckAutoOrder = this;
                        frm.UserType = 2;                   //表示是自动开单审核
                        editTimes = 2;
                        frm.ShowDialog();
                        Cursor = Cursors.Default;
                    }
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        //删除订单
        private void tsDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridViewOrder.SelectedRowsCount > 0)
                {
                    if (DialogResult.Yes == MessageBox.Show("确定要删除选中的订单吗？", "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                    {
                        List<int> orderIds = new List<int>();               //待删除的订单Id集合
                        int[] indexes = gridViewOrder.GetSelectedRows();
                        foreach (int index in indexes)
                        {
                            orderIds.Add(Convert.ToInt32(gridViewOrder.GetRowCellValue(index, "OrderBagID")));
                        }
                        bll.DelOrder(orderIds);
                        //刷新
                        BindData();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //---------------公共方法-----------------
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            List<BLL.CustomerManage.OrderBagEntity> orders = bll.GetOrders((int)COM.OrderBagStatus.AutoStartOrder);
            gridControlOrders.DataSource = orders;
            gridControlOrders.RefreshDataSource();
            lblOrderCount.Text = gridViewOrder.RowCount.ToString();
        }

        #region 审核通过
        private void CheckAcross(List<int> orderIds)
        {
            try
            {
                if (orderIds == null || orderIds.Count == 0)
                    return;
                bll.CheckAcross(orderIds);
                BindData();                     //刷新
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除指定的订单
        private void DelOrders(List<int> orderIds)
        {
            try
            {

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
